package com.squirrels.server.utils;

import com.google.protobuf.ByteString;
import com.squirrels.server.common.constant.Constant;
import com.squirrels.server.message.protobuf.Message;

/**
 * 创建者: FlyingSquirrels
 * 创建时间: 2024/8/1 16:03
 * 描述:
 */
public class EntityUtils {

    /**
     * 常用信息包
     * @param entity 前端传过来的操作目录 一级 二级
     * @param constant 返回code和info
     * @param data 返回数据
     * @return
     */
    public static Message.Entity buildResData(Message.Entity entity,Constant constant,ByteString data,String... args){
        String token = "";
        if (args.length >= 1){
            token = args[0];
        }
        return buildAllData(
                (int)constant.getValue(),
                constant.getInfo(),
                data,
                token,
                entity.getOperation(),
                entity.getOperationBase());
    }

    // 创建完整信息包
    public static Message.Entity buildAllData(int code,String info, ByteString data, String token,String operation,String operationBase){
        if (null == data) {
            data = ByteString.empty();
        }
        Message.Data.Builder builder = Message.Data.newBuilder()
                .setInfo(info)
                .setBin(data);
        Message.Entity entity = Message.Entity.newBuilder()
                .setDataType(Message.Entity.EntityType.DataType)
                .setData(builder)
                .setCode(code)
                .setToken(token)
                .setOperation(operation)
                .setOperationBase(operationBase)
                .build();
        return entity;

    }

}
